/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EliminarEmisora.java
 *
 * Created on 26/01/2012, 07:27:40 AM
 */
package GUI;

import Control.EmisoraJpaController;
import Control.exceptions.IllegalOrphanException;
import Control.exceptions.NonexistentEntityException;
import Entidad.Emisora;
import java.awt.PopupMenu;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pepe
 */
public class EliminarEmisora extends javax.swing.JPanel {

    /** Creates new form EliminarEmisora */
    public EliminarEmisora() {
        initComponents();
        controlEmisora = new EmisoraJpaController();
    }

    EmisoraJpaController controlEmisora ;
    Emisora e;
    boolean encontrado = false;
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        buscarJB = new javax.swing.JButton();
        statusJL = new javax.swing.JLabel();
        cancelarJB = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ciudadJL = new javax.swing.JLabel();
        dialJL = new javax.swing.JLabel();
        eliminarJB = new javax.swing.JButton();

        jLabel1.setText("Nombre");

        nombreTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreTFActionPerformed(evt);
            }
        });

        buscarJB.setText("Buscar");
        buscarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarJBActionPerformed(evt);
            }
        });

        cancelarJB.setText("Cancelar");
        cancelarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarJBActionPerformed(evt);
            }
        });

        jLabel2.setText("Ciudad");

        jLabel3.setText("Dial");

        eliminarJB.setText("Eliminar");
        eliminarJB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarJBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(statusJL, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nombreTF, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(123, 123, 123)
                .addComponent(buscarJB)
                .addGap(18, 18, 18)
                .addComponent(cancelarJB)
                .addContainerGap(109, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dialJL, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addComponent(ciudadJL, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(174, Short.MAX_VALUE)
                .addComponent(eliminarJB)
                .addGap(157, 157, 157))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buscarJB, cancelarJB});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buscarJB)
                    .addComponent(cancelarJB))
                .addGap(11, 11, 11)
                .addComponent(statusJL, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(ciudadJL, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(dialJL, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(eliminarJB)
                .addContainerGap(102, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void nombreTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nombreTFActionPerformed
        
}//GEN-LAST:event_nombreTFActionPerformed

private void buscarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarJBActionPerformed
       
    e = controlEmisora.buscarEmisoraPorNombre(nombreTF.getText());
    
    if(nombreTF.getText().equals("")){
        statusJL.setText("El nombre no puede estar vacio");
    }else{
        if(e == null){
            statusJL.setText("No se pudo encontrar la emisora");
        }else{
            statusJL.setText("Encontrado");
            ciudadJL.setText(e.getCiudad());
            dialJL.setText(e.getDial()+"");
            encontrado = true;
        }
    }
            
}//GEN-LAST:event_buscarJBActionPerformed

private void cancelarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarJBActionPerformed
    nombreTF.setText("");
    statusJL.setText("");
    ciudadJL.setText("");
    dialJL.setText("");
    encontrado=false;
}//GEN-LAST:event_cancelarJBActionPerformed

private void eliminarJBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarJBActionPerformed
    if(encontrado){
            try {
                controlEmisora.destroy(e.getIdEmisora());
                encontrado = false;
                statusJL.setText("La emisora se ha eliminado correctamente");
            } catch (IllegalOrphanException ex) {
                Logger.getLogger(EliminarEmisora.class.getName()).log(Level.SEVERE, null, ex);
                statusJL.setText("La emisora no pudo ser eliminada");
            } catch (NonexistentEntityException ex) {
                statusJL.setText("La emisora no pudo ser eliminada");
                Logger.getLogger(EliminarEmisora.class.getName()).log(Level.SEVERE, null, ex);
            }     
    }else{
        statusJL.setText("No hay emisora a eliminar");
    }
}//GEN-LAST:event_eliminarJBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarJB;
    private javax.swing.JButton cancelarJB;
    private javax.swing.JLabel ciudadJL;
    private javax.swing.JLabel dialJL;
    private javax.swing.JButton eliminarJB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JLabel statusJL;
    // End of variables declaration//GEN-END:variables
}
